Home:ALL Converter>how to change print in the end of the function for return?

how to change print in the end of the function for return?

Ask Time:2020-02-24T01:19:50         Author:Roitko

Json Formatter

I found a sudoku solver code however, I need the function to return the grid instead of printing it. Any idea how?

import numpy as np

grid = [[5,3,0,0,7,0,0,0,0],
        [6,0,0,1,9,5,0,0,0],
        [0,9,8,0,0,0,0,6,0],
        [8,0,0,0,6,0,0,0,3],
        [4,0,0,8,0,3,0,0,1],
        [7,0,0,0,2,0,0,0,6],
        [0,6,0,0,0,0,2,8,0],
        [0,0,0,4,1,9,0,0,5],
        [0,0,0,0,0,0,7,0,0]
            ]

def checker(y,x,n):
    global grid
    for i in range(0,9):
        if grid[y][i] == n:
            return False
    for i in range(0,9):
        if grid[i][x] == n:
            return False
    x0 = (x//3)*3
    y0 = (y//3)*3
    for i in range(0,3):
        for j in range(0,3):
            if grid[y0+i][x0+j] == n:
                return False
    return True

def solver(grid):
    for y in range(9):
        for x in range(9):
            if grid[y][x] == 0:
                for n in range(1,10):
                    if checker(y,x,n):
                        grid[y][x] = n
                        solver(grid)
                        grid[y][x] = 0
                return
    print(np.matrix(grid))
    input("More?")

solver(grid)

This function does give you the correct solution however, I would like to call this function in my other project but I am not able to as the output is a print and not the grid itself.

Thanks!

Author:Roitko,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60364811/how-to-change-print-in-the-end-of-the-function-for-return
yy